package me.magicall.web.simple.spring.controllers;

import javax.servlet.http.HttpServletRequest;

import me.magicall.db.util.PageInfo.PageMode;
import me.magicall.web.simple.spring.util.C;
import me.magicall.web.simple.spring.util.UrlPatternRule;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * rest风格url设计：
 * 第一段是model的类名，也是controller的前半截名字
 * 第二段是数据库操作方式（one、list、edit、update、del），为one时可以省略；
 * 第三段是id或其他参数；
 * // GET /tag/${id}
 * // GET /tag/edit/${id}
 * GET /tag/${id}/edit
 * // POST /tag/update/${id}
 * POST /tag/${id}
 * // POST /tag/del/${id}
 * DELETE /tag/${id}
 * // GET /tag/list/1~2
 * GET /tags/1~2
 * // GET /tag/list/1,2,3
 * // GET /tag/list?tag.name=xx&tag.addTime=xx
 * 
 * @author MaGiCalL
 */
@RequestMapping(method = RequestMethod.GET, value = { "" })
@Controller("modelIndexController")
public class ModelIndexController {

	@Autowired
	private ListController listController;

	/**
	 * 某模块的首页，默认为list第一页
	 * 
	 * @param request
	 * @param modelMap
	 * @param modelName
	 * @return
	 */
	@RequestMapping(UrlPatternRule.SINGULAR_MODEL_NAME)
	public String index(final HttpServletRequest request, final ModelMap modelMap,//
			@PathVariable("modelName") final String modelName) {
		return listController.listPaged(request, modelMap, modelName, getDefaultPageMode().firstPage);
	}

	protected PageMode getDefaultPageMode() {
		return C.DEFAULT_PAGE_MODE;
	}
}
